package common.correspondence;

import ap.BDDACLWrapper;
import jdd.bdd.BDD;
import java.util.Arrays;


public class BDDPrintTransfer extends BDDTransferAbstract implements BDDTransfer {
    final BDD manager;

    public BDDPrintTransfer(BDD manager){
        this.manager = manager;
    }

    @Override
    public String bddToString(int bddIndex) {
        return String.join(" ", manager.printSetString(bddIndex));
    }

    @Override
    public int stringToBdd(String m) {
        String[] sl = m.split("\\s");
        return stringToBdd(Arrays.asList(sl));
    }

    @Override
    public int stringToBdd(Iterable<String> sl) {
        int res = manager.getZero();
        synchronized (manager){
            for(String s: sl){
                int r;
                if(s.equals("FALSE") || s.equals("TRUE")){
                    r = s.equals("TRUE")?BDDACLWrapper.BDDTrue:BDDACLWrapper.BDDFalse;
                }else{
                    r = manager.ref(manager.minterm(s));
                }
                res = manager.orTo(res, r);
                manager.deref(r);
            }
        }
        return res;
    }

}
